﻿using System;
using System.Collections.Generic;
using System.Windows;
using Yxinmt.Interface;
using Yxinmt.Utility;

namespace Yxinmt.Windows.Meeting
{
    public class WebPage : BaseWindow, IWindow
    {
        public Index _Index { get; set; }
        public WebPage(Window window, Dictionary<string, object> parameters)
        {
            Window = window;
            _Parameters = parameters;
            IsDialogWindow = true;
            _Index = parameters.GetValue<Index>("Index");

            InitSet(GetType().Name);

            initView();

            this.SetWindow();

            initLoad();
        }

        void initView()
        {
            var webView1 = Utility.Common.GetProperty(_View, "WebView1");
            webView1["Url"] = _Parameters.GetStringValue("Url");
        }

        Controls.WebView2 _WebView1 { get; set; }

        void initLoad()
        {
            _WebView1 = (Controls.WebView2)GetControl("WebView1");

            _WebView1.ReceiveMessage = (msg) =>
            {
                string type = msg.GetStringValue("type");
                Dictionary<string, object> data = msg.GetValue<Dictionary<string, object>>("data");
                if (type == "closeDialog") this.Window.DialogResult = true;
                else if (type == "startShare") startShare(data);
                else if (type == "endShare") endShare();
                else if (type == "receiveStartShare") receiveStartShare(data);
            };

            setFilterResponse();
        }

        void startShare(Dictionary<string, object> data)
        {
            _Index.StartShare(data);
            this.Window.DialogResult = true;
        }

        void endShare()
        {
            _Index.EndShare();
            this.Window.DialogResult = true;
        }
        void receiveStartShare(Dictionary<string, object> data)
        {
            _Index.ReceiveStartShare(data);
            this.Window.DialogResult = true;
        }
        
        void setFilterResponse()
        {
            _WebView1.Filters = new List<string>()
            {
                "http://*/api/VisitUser2/GetUserInfo?*"
            };

            _WebView1.SetResourceResponse = (res) => SetResourceResponse(res);
        }

        void SetResourceResponse(Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequestedEventArgs e)
        {
            bool isGetUserInfo = e.Request.Uri.ToLower().Contains("visituser2/getuserinfo");

            Dictionary<string, object> dict = new Dictionary<string, object>();

            string headers = "Content-Type:application/json;charset=utf-8";
            var coreWebView2 = _WebView1.GetCoreWebView2();
            try
            {
                if (isGetUserInfo)
                {
                    dict["Data"] = _Index.UserInfo;
                    dict["Token"] = RequestService.Token;
                    dict["Result"] = true;
                }
            }
            catch (Exception ex)
            {
                dict.Add("Message", ex.Message);
                if (!string.IsNullOrEmpty(RequestService.Token)) dict["Token"] = RequestService.Token;
                dict["Result"] = false;
                LoggerProxy.Exception("Meeting.Index", "SetResourceResponse", ex);
            }
            System.IO.MemoryStream ms = Utility.Common.StringToMemoryStream(Utility.Common.ToJson(dict));
            e.Response = coreWebView2.Environment.CreateWebResourceResponse(ms, 200, "OK", headers);
        }

    }
}
